package com.adwszzy;

//import com.sun.jna.Native;
//import com.sun.jna.platform.win32.WinDef.HHOOK;
//import com.sun.jna.platform.win32.WinDef.LPARAM;
//import com.sun.jna.platform.win32.WinDef.WPARAM;
//import com.sun.jna.platform.win32.WinUser;
//import com.sun.jna.platform.win32.WinUser.HookProc;
//import com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;
import com.melloware.jintellitype.JIntellitypeConstants;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.io.File;

import static com.adwszzy.ScreenMatcher.matchAndlockTheSoundSkeleton;

public class GlobalKeyListener {
    public static final int PACK_FUNK_KEY = 1;
    public static final int SYNTHESIS_FUNK_KEY = 2;
    public static final int CLEAR_TAGS_FUNK_KEY = 3;
    public static final int STOP_KEY = 4;
    public static final int EXIT_KEY = 5;

    private static HotkeyListener hotKeyListener;
    private static JIntellitype jIntellitype = JIntellitype.getInstance();
    private static Thread thread = null;
    public static void main(String[] args) {
        registerGlobalKeyboardHook();
    }

    private static void registerGlobalKeyboardHook() {
        File f = new File("");
        System.out.println(f.getAbsolutePath());
        jIntellitype.registerHotKey(PACK_FUNK_KEY, JIntellitypeConstants.MOD_ALT+JIntellitypeConstants.MOD_SHIFT,(int)'6');
        jIntellitype.registerHotKey(SYNTHESIS_FUNK_KEY, JIntellitypeConstants.MOD_ALT+JIntellitypeConstants.MOD_SHIFT,(int)'7');
        jIntellitype.registerHotKey(CLEAR_TAGS_FUNK_KEY, JIntellitypeConstants.MOD_ALT+JIntellitypeConstants.MOD_SHIFT,(int)'8');
        jIntellitype.registerHotKey(STOP_KEY, JIntellitypeConstants.MOD_ALT+JIntellitypeConstants.MOD_SHIFT,(int)'9');
        jIntellitype.registerHotKey(EXIT_KEY, JIntellitypeConstants.MOD_ALT+JIntellitypeConstants.MOD_SHIFT,(int)'0');
        hotKeyListener = identifier -> {
            switch (identifier) {
                case PACK_FUNK_KEY:
                    System.out.println("检查背包声骸");
                    ScreenMatcher.execFlag = false;
                    stopThread();
                    ScreenMatcher.execFlag = true;
                    thread = new Thread(new ScreenMatcherThread(0));
                    thread.start();
                    break;

                case SYNTHESIS_FUNK_KEY:
                    System.out.println("幸运五合一");
                    ScreenMatcher.execFlag = false;
                    stopThread();
                    ScreenMatcher.execFlag = true;
                    thread = new Thread(new ScreenMatcherThread(1));
                    thread.start();
                    break;

                case CLEAR_TAGS_FUNK_KEY:
                    System.out.println("将背包中的new标签全部点掉");
                    ScreenMatcherThread.mode = 1;

                    break;
                case STOP_KEY:
                    System.out.println("停止！");
                    ScreenMatcher.execFlag = false;
                    stopThread();
                    break;
                case EXIT_KEY:
                    System.out.println("退出！");
                    ScreenMatcher.execFlag = false;
                    stopThread();
                    stopListening();
                    System.exit(0);
                    break;
                default:
                    break;
            }
        };
        jIntellitype.addHotKeyListener(hotKeyListener);
    }

    private static void stopThread() {
        if (thread==null)return;
        System.out.println("停止线程");
        thread.interrupt();
        while (thread.getState() != Thread.State.TERMINATED) {
            // 等待线程结束
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    private static void stopListening() {
        jIntellitype.removeHotKeyListener(hotKeyListener);
    }


    /**
     * 模拟鼠标滚轮滚动
     *
     * @param robot Robot对象
     * @param wheelAmount 滚动的单位数量，正值表示向上滚动，负值表示向下滚动
     */
    private static void scrollMouseWheel(Robot robot,  int wheelAmount) {
        // 模拟按下滚轮
//        robot.mousePress(wheelButton);
        // 模拟滚轮滚动
        for (int i = 0; i < Math.abs(wheelAmount); i++) {
            if (wheelAmount > 0) {
                robot.mouseWheel(1); // 向上滚动
            } else {
                robot.mouseWheel(-1); // 向下滚动
            }
        }
        // 模拟释放滚轮
//        robot.mouseRelease(wheelButton);
    }
}